package com.sdkx.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.model.WayPoint;

import java.util.List;


public class WpListAdapter extends BaseAdapter implements SectionIndexer {
    private Context context;
    private List<WayPoint> wplist;
//    private List<ViewHolder> vlist;

    public WpListAdapter(Context context, List<WayPoint> list) {
        this.context = context;
        this.wplist = list;

    }

    private int selectpos=-1;
    public void setSelectedPosition(int position) {
        selectpos = position;
    }
    private ViewHolder thisholder = null;
    @Override
    public int getCount() {
        return wplist.size();
    }

    @Override
    public Object getItem(int i) {
        return wplist.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.wplist_item, null);
            holder = new ViewHolder(convertView);
            holder.tvWpname.setTag(holder);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tvWpname.setText(wplist.get(position).getName());
        return convertView;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public List<WayPoint> getList() {
        return wplist;
    }

    public void setList(List<WayPoint> list) {
        this.wplist = list;
    }

    @Override
    public Object[] getSections() {
        return new Object[0];
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        //TODO：数据库接口更改，需要重新编写代码逻辑

//        for (int i = 0; i < getCount(); i++) {
//            String sortStr = wplist.get(i).getSortLetters();
//            char firstChar = sortStr.toUpperCase().charAt(0);
//            if (firstChar == sectionIndex) {
//                return i;
//            }
//        }

        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
        return 0;
    }

    protected class ViewHolder {
        private TextView tvWpname;

        ViewHolder(View view) {
            tvWpname = (TextView) view.findViewById(R.id.tv_wpname);
        }
    }

}
